package com.imay.live.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.base.util.ViewUtil;
import com.imay.live.R;
import com.imay.live.activity.RoomLiveShortActivity;
import com.imay.live.activity.discovery.SupperDekaronActivity;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.helper.UrlHelper;
import com.imay.live.model.ShortLiveInfoModel;
import com.imay.live.model.SupperModel;
import com.imay.live.model.TopicModel;
import com.imay.live.view.SupperScrollView;
import com.imay.live.view.WebpView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liangzhenxiong on 2017/4/6 0006.
 */

public class SupperDekaronAdapter extends BaseQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static float w = 0;
    private int h = 0;
    public static int padding;
    public static float sum = 3.5f;
    public static int maxsum = 3;

    public SupperDekaronAdapter(List<MultiItemEntity> data, Context context) {
        super(R.layout.item_shortlive_info, data);
        padding = (int) context.getResources().getDimension(R.dimen.dp_4);
        w = (ViewUtil.mScreemWidth - (padding * 4)) / sum;
        h = (int) (w * 4 / 3);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity o) {
        if (o instanceof SupperModel) {
            final SupperModel item = (SupperModel) o;
            LinearLayout linearLayout = helper.getView(R.id.linear);
            SupperScrollView supperScrollView = helper.getView(R.id.scrollView);
            supperScrollView.scrollTo(0, 0);
            if (item.getTopic() != null) {
                helper.setText(R.id.tv_title, item.getTopic().getTitle());
                helper.setText(R.id.tv_sum, String.valueOf(item.getTopic().getSuccessNumber()));
                helper.addOnClickListener(R.id.re_title);
                helper.addOnClickListener(R.id.iv_dekaron);
                helper.setVisible(R.id.iv_dekaron, item.getTopic().getMedal() == 1);
            }

            for (int i = 0; i < 10; i++) {
                WebpView imageView;
                if (linearLayout.getChildCount() > i) {
                    imageView = (WebpView) linearLayout.getChildAt(i);
                    imageView.setAntion(View.GONE);
                } else {
                    imageView = new WebpView(helper.getConvertView().getContext());
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) w, h);
                    params.leftMargin = padding;
                    linearLayout.addView(imageView, params);
                }
                if (item.getShortLiveInfos().size() <= i) {
                    imageView.setVisibility(View.GONE);
                    imageView.setAntion(View.GONE);
                } else {
                    String image;
//                    item.getShortLiveInfos().get(i).setFaceUrl("http://192.168.30.13:8080/img/100.webp");
                    if (TextUtils.isEmpty(item.getShortLiveInfos().get(i).getFaceUrl())) {
                        image = UrlHelper.getVideoThumbUrl(item.getShortLiveInfos().get(i).getVideoUrl());
                        if (i < sum) {
                            imageView.setUri(image, (int) w, h);
                        }
                    } else {
                        image = item.getShortLiveInfos().get(i).getFaceUrl();
                        if (!image.endsWith("webp")) {
                            image = UrlHelper.getVideoThumbUrl(item.getShortLiveInfos().get(i).getVideoUrl());
                        }
                        if (i < sum) {
                            imageView.setUri(image, i != 3, (int) w, h);
                        }
                    }
                    if (i >= sum) {
                        imageView.setTag(R.id.iv_supper_image, image);
                    }

                    final int finalI = i;
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            List<ShortLiveInfoModel> supperModelTypes = (List<ShortLiveInfoModel>) item.getShortLiveInfos().subList(0, finalI);
                            List<Long> infoModels = new ArrayList<Long>();
                            for (ShortLiveInfoModel shortLiveInfoModel : supperModelTypes) {
                                infoModels.add(shortLiveInfoModel.getShortLiveId());
                            }
                            infoModels.add(item.getShortLiveInfos().get(finalI).getShortLiveId());
                            RoomLiveShortActivity.startActivity(helper.getConvertView().getContext(), infoModels, item.getShortLiveInfos().get(finalI).getChallengeId(), 0);
                            MobclickAgent.onEvent(mContext, "neirong_click");

                        }
                    });
                }

            }
            if (linearLayout.getChildCount() == 10) {
                TextView textView = new TextView(helper.getConvertView().getContext());
                int left = (int) helper.getConvertView().getResources().getDimension(R.dimen.dp_6);
                int right = (int) helper.getConvertView().getResources().getDimension(R.dimen.dp_10);
                textView.setPadding(left, 0, right, 0);
                textView.setGravity(Gravity.CENTER);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, h);
                textView.setText(helper.getConvertView().getResources().getString(R.string.str_right_more));
                linearLayout.addView(textView, params);
            }
            supperScrollView.setScrollOver(new SupperScrollViewOverListener(item.getTopic()));

        }
    }
    class SupperScrollViewOverListener implements SupperScrollView.ScrollOver{

        TopicModel topic;
        public SupperScrollViewOverListener( TopicModel topic){
            this.topic=topic;
        }

        @Override
        public void srollover() {
            if (topic== null|| mContext ==null) {
                return;
            }
            SupperDekaronActivity.start(mContext,topic);
        }
    }

}
